﻿using Avalonia;
using Avalonia.Animation;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Interactivity;
using AvaloniaApplication1.ViewModels;
using System;
using System.Threading.Tasks;

namespace AvaloniaApplication1;

public class BasePage : UserControl
{
    /// <summary>
    /// The time any slide animation takes to complete
    /// </summary>
    public float SlideSeconds { get; set; } = 0.3f;

    /// <summary>
    /// A flag to indicate if this page should animate out on load.
    /// Useful for when we are moving the page to another frame
    /// </summary>
    public bool ShouldAnimateOut { get; set; }

    /// <summary>
    /// The View Model associated with this page
    /// </summary>
    private object? _viewModel;

    /// <summary>
    /// The View Model associated with this page
    /// </summary>
    public object? ViewModelObject
    {
        get => _viewModel;
        set
        {
            // If nothing has changed, return
            if (_viewModel == value)
                return;

            // Update the value
            _viewModel = value;

            // Fire the view model changed method
            OnViewModelChanged();

            // Set the data context for this page
            DataContext = _viewModel;
        }
    }

    /// <summary>
    /// Default constructor
    /// </summary>
    public BasePage()
    {

    }

    protected override void OnInitialized()
    {
        base.OnInitialized();

     
    }

    protected override void OnLoaded(RoutedEventArgs e)
    {

    }

    /// <summary>
    /// Animates the page in
    /// </summary>
    /// <returns></returns>
    private void AnimateIn()
    {
   
    }

    /// <summary>
    /// Animates the page out
    /// </summary>
    /// <returns></returns>
    private void AnimateOut()
    {
        var size = ((IClassicDesktopStyleApplicationLifetime)Application.Current.ApplicationLifetime).MainWindow.Bounds
            .Width;

        var offset = size == 0 ? Bounds.Width : size;

        Opacity = 0;
        Margin = new Thickness(-offset, 0, offset, 0);
    }

    /// <summary>
    /// Fired when the view model changes
    /// </summary>
    protected virtual void OnViewModelChanged()
    {
    }
}

public class BasePage<VM> : BasePage
    where VM : ViewModelBase, new()
{
    #region Public Properties

    /// <summary>
    /// The view model associated with this page
    /// </summary>
    public VM? ViewModel
    {
        get => (VM?)ViewModelObject;
        set => ViewModelObject = value;
    }

    #endregion

    #region Constructor

    public BasePage()
    {
        // Create a default view model
        ViewModel = new VM();
    }

    /// <summary>
    /// Constructor with specific view model
    /// </summary>
    /// <param name="specificViewModel">The specific view model to use, if any</param>
    public BasePage(VM? specificViewModel = null) : base()
    {
        // Set specific view model
        if (specificViewModel != null)
            ViewModel = specificViewModel;
        else
        {
            // Create a default view model
            ViewModel = new VM();
        }
    }

    #endregion
}